package cn.cmcc.online.smsapi.ui.view;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import org.json.JSONObject;

import java.util.List;

import cn.cmcc.online.smsapi.ui.R;
import cn.cmcc.online.smsapi.ui.model.MetaData;
import cn.cmcc.online.smsapi.ui.model.TableData;
import cn.cmcc.online.smsapi.ui.model.TrainData;
import cn.cmcc.online.smsapi.ui.model.TransportSelectInfo;
import cn.cmcc.online.smsapi.ui.presenter.TrainPresenter;
import cn.cmcc.online.smsapi.ui.util.ContentUtil;
import cn.cmcc.online.smsapi.ui.widget.TransportSelectDialog;
import hugo.weaving.DebugLog;

public class TrainVistaImpl extends GeneralBaseVistaImpl implements TrainVista {
    private static final String TAG = "TrainVistaImpl";

    private Activity mActivity;
    private TextView mTextViewTrainNumber;
    private TextView mTextViewDepartAddress;
    private TextView mTextViewArriveAddress;
    private TextView mTextViewDepartTime;
    private TextView mTextViewArriveTime;
    private TextView mTextViewDepartDate;
    private TextView mTextViewArriveDate;
    private TextView mTextViewSeatInfoTitle;
    private TextView mTextViewSeatInfo;
    private TextView mTextViewTableTitle1;
    private TextView mTextViewTableInfo1;
    private TextView mTextViewTableTitle2;
    private TextView mTextViewTableInfo2;
    private TextView mTextViewCheckInPortTitle;
    private TextView mTextViewCheckInPort;
    private TextView mTextViewWarnInfo;

    private View mImageViewDottedSplitLine;

    private TrainPresenter mTrainPresenter = null;
    private TransportSelectDialog mDialog;

    public TrainVistaImpl(Context context, TrainPresenter presenter) {
        super(context);

        mTrainPresenter = presenter;
        mTrainPresenter.setVista(this);

        initView();
    }

    // MUST put after parent class constructor
    private void initView() {
//        mTextViewTrainNumber.setOnClickListener(mSelectTrainClickListener);
        mTextViewArriveAddress.setOnClickListener(mSelectStationClickListener);
    }

    @Override
    @DebugLog
    protected void initLayout() {
        super.initLayout();
        super.addBody(View.inflate(mContext, R.layout.bubble_train_body, null));

        mTextViewTrainNumber = findView(R.id.bubble_train_number);

        mTextViewDepartAddress = findView(R.id.bubble_depart_city);
        mTextViewArriveAddress = findView(R.id.bubble_arrive_city);

        mTextViewDepartTime = findView(R.id.bubble_depart_time);
        mTextViewArriveTime = findView(R.id.bubble_arrive_time);
        mTextViewDepartDate = findView(R.id.bubble_depart_date);
        mTextViewArriveDate = findView(R.id.bubble_arrive_date);

        mTextViewSeatInfoTitle = findView(R.id.bubble_train_seat_info_title);
        mTextViewSeatInfo = findView(R.id.bubble_train_seat_info);


        mTextViewTableTitle1 = findView(R.id.train_table_line_1_title);
        mTextViewTableInfo1 = findView(R.id.train_table_line_1_info);
        mTextViewTableTitle2 = findView(R.id.train_table_line_2_title);
        mTextViewTableInfo2 = findView(R.id.train_table_line_2_info);

        mTextViewCheckInPortTitle = findView(R.id.bubble_train_checkin_port_title);
        mTextViewCheckInPort = findView(R.id.bubble_train_checkin_port_content);

        mTextViewWarnInfo = findView(R.id.warm_train_info);
        mImageViewDottedSplitLine = findView(R.id.bubble_dotted_split_imm);
    }

    private View.OnClickListener mSelectTrainClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mTrainPresenter.queryTrainNumber();
        }
    };

    private View.OnClickListener mSelectStationClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mTrainPresenter.queryTrainStation();
        }
    };

    @DebugLog
    @Override
    public void bind(Context context, JSONObject data) {
        mActivity = (Activity) context;
        initDefaultViewInner();
        mTrainPresenter.bind(data);
        super.bind(context, data);
    }

    // TrainVista start
    @Override
    @DebugLog
    public void showBody(TrainData data, MetaData metaData) {
        if (data == null) {
            return;
        }

        if (metaData != null && metaData.getHighlightColor() != null && !"".equalsIgnoreCase(metaData.getHighlightColor())) {
            Drawable selectDrawable = mContext.getResources().getDrawable(R.drawable.bubble_select_train_unfold);
            selectDrawable = ContentUtil.setDrawableColorFilter(selectDrawable, metaData.getHighlightColor());
            mTextViewArriveAddress.setCompoundDrawablesWithIntrinsicBounds(null, null, selectDrawable, null);
            mTextViewArriveAddress.setCompoundDrawablePadding(mContext.getResources().getDimensionPixelOffset(R.dimen.bubble_train_select_img_margin_left));
            mTextViewArriveAddress.setTextColor(Color.parseColor(metaData.getHighlightColor()));
        }

        if (!TextUtils.isEmpty(data.getTrainNumber())) {
            mTextViewTrainNumber.setText(data.getTrainNumber());
        }

        if (!TextUtils.isEmpty(data.getDepartAddress())) {
            mTextViewDepartAddress.setText(data.getDepartAddress());
        }

        if (!TextUtils.isEmpty(data.getArriveAddress())) {
            mTextViewArriveAddress.setText(data.getArriveAddress());
        } else {
            mTextViewArriveAddress.setText(ContentUtil.NO_CITY);
        }

        if (!TextUtils.isEmpty(data.getDepartTime())) {
            mTextViewDepartTime.setText(data.getDepartTime());
        }

        if (!TextUtils.isEmpty(data.getArriveTime())) {
            mTextViewArriveTime.setText(data.getArriveTime());
        } else {
            mTextViewArriveTime.setText(ContentUtil.NO_TIME);
        }

        if (!TextUtils.isEmpty(data.getDepartDate())) {
            mTextViewDepartDate.setText(data.getDepartDate());
        }

        if (!TextUtils.isEmpty(data.getArriveDate())) {
            mTextViewArriveDate.setText(data.getArriveDate());
        } else {
            mTextViewArriveDate.setText(ContentUtil.NO_DATE);
        }

        if (!TextUtils.isEmpty(data.getSeatInfoTitle())) {
            mTextViewSeatInfoTitle.setText(data.getSeatInfoTitle());
        }

        if (!TextUtils.isEmpty(data.getSeatInfo())) {
            mTextViewSeatInfo.setText(data.getSeatInfo());
        } else {
            mTextViewSeatInfo.setText(ContentUtil.NO_DATA);
        }

        if (!TextUtils.isEmpty(data.getCheckPort())) {
            mTextViewCheckInPort.setText(data.getCheckPort());
            mTextViewCheckInPort.setVisibility(View.VISIBLE);
            mTextViewCheckInPortTitle.setVisibility(View.VISIBLE);
        } else {
            mTextViewCheckInPort.setVisibility(View.GONE);
            mTextViewCheckInPortTitle.setVisibility(View.GONE);
        }
    }

    @Override
    public void enableTrainSelect(MetaData metaData) {
        if (mTextViewTrainNumber != null && !TextUtils.isEmpty(metaData.getHighlightColor())) {
            Drawable selectDrawable = mContext.getResources().getDrawable(R.drawable.bubble_select_train_unfold);
            selectDrawable = ContentUtil.setDrawableColorFilter(selectDrawable, metaData.getHighlightColor());
            mTextViewTrainNumber.setCompoundDrawablesWithIntrinsicBounds(null, null, selectDrawable, null);
            mTextViewTrainNumber.setCompoundDrawablePadding(mContext.getResources().getDimensionPixelOffset(R.dimen.bubble_train_select_img_margin_left));
            mTextViewTrainNumber.setTextColor(Color.parseColor(metaData.getHighlightColor()));
            mTextViewTrainNumber.setOnClickListener(mSelectTrainClickListener);
        }
    }

    @Override
    public void disableTrainSelect() {
        if (mTextViewTrainNumber != null) {
            mTextViewTrainNumber.setTextColor(mContext.getResources().getColor(R.color.bubble_train_num_disanble));
            mTextViewTrainNumber.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
            mTextViewTrainNumber.setOnClickListener(null);
        }
    }

    @Override
    public void showTrainSelect(TransportSelectInfo selectInfo, int selected) {
        if (mDialog == null) {
            mDialog = new TransportSelectDialog(mActivity, selectInfo, selected);
            mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    mTrainPresenter.selectTrain(mDialog.getSelected());
                }
            });
        }
        if (!mActivity.isFinishing()) {
            mDialog.show();
        }
    }

    @Override
    public void initDefaultView() {
        Log.i(TAG, "initDefaultView: ");
        super.initDefaultView();
    }

    @DebugLog
    private void initDefaultViewInner() {
//        disableTrainSelect();

        mTextViewDepartTime.setText(ContentUtil.NO_TIME);
        mTextViewArriveTime.setText(ContentUtil.NO_TIME);
        mTextViewDepartDate.setText(ContentUtil.NO_DATE);
        mTextViewArriveDate.setText(ContentUtil.NO_DATE);
        mTextViewDepartAddress.setText(ContentUtil.NO_DATA);
        mTextViewSeatInfo.setText(ContentUtil.NO_DATA);
        mTextViewArriveAddress.setText(ContentUtil.NO_CITY);
        mTextViewCheckInPort.setText(ContentUtil.NO_DATA);

        mTextViewWarnInfo.setText(null);
        mImageViewDottedSplitLine.setVisibility(View.GONE);
        mTextViewWarnInfo.setVisibility(View.GONE);
    }

    @Override
    @DebugLog
    public void showTable(List<TableData> dataList) {
        if (dataList.size() >= 1) {
            TableData data1 = dataList.get(0);
            mTextViewTableTitle1.setText(data1.getKey());
            mTextViewTableInfo1.setText(data1.getValue());
            mTextViewTableTitle1.setVisibility(View.VISIBLE);
            mTextViewTableInfo1.setVisibility(View.VISIBLE);
        }
        if (dataList.size() >= 2) {
            TableData data2 = dataList.get(1);
            mTextViewTableTitle2.setText(data2.getKey());
            mTextViewTableInfo2.setText(data2.getValue());
            mTextViewTableTitle2.setVisibility(View.VISIBLE);
            mTextViewTableInfo2.setVisibility(View.VISIBLE);
        }
    }

    @Override
    @DebugLog
    public void showNote(String note) {
        mTextViewWarnInfo.setText(note);
        mImageViewDottedSplitLine.setVisibility(View.VISIBLE);
        mTextViewWarnInfo.setVisibility(View.VISIBLE);
    }

    //TODO is usable?
    @Override
    public void showStationSelect(TransportSelectInfo selectInfo, int selected) {
//        if (mDialog == null) {
//            mDialog = new TransportSelectDialog(mActivity);
//            mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
//                @Override
//                public void onDismiss(DialogInterface dialog) {
//                    mTrainPresenter.selectStation(mDialog.getSelected());
//                }
//            });
//        }
//        if (!mActivity.isFinishing()) {
//            mDialog.bindData(selectInfo, selected);
//            mDialog.show();
//        }
    }

    @Override
    public void updateBottom() {
        super.updateBottom();
    }
}
